import time
import hashlib


class BookSpiderMiddleware:

    def process_request(self, request, spider):
        # 讯代理 的代理ip配置
        # 订单号
        orderno = "ZF202012202203B6gxPF"
        # 密钥
        secret = "a6df16b19a0f4398af9c0e9c0f0987b3"
        request.meta['proxy'] = 'http://forward.xdaili.cn:80'
        timestamp = str(int(time.time()))  # timestamp
        string = "orderno=" + orderno + "," + "secret=" + secret + "," + "timestamp=" + timestamp
        md5_string = hashlib.md5(string.encode('utf-8')).hexdigest()  # sign
        sign = md5_string.upper()
        auth = "sign=" + sign + "&" + "orderno=" + orderno + "&" + "timestamp=" + timestamp
        request.headers["Proxy-Authorization"] = auth
        return None

